package co.yixiang.modules.user.rest;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.api.ApiResult;
import co.yixiang.api.YshopException;
import co.yixiang.common.bean.LocalUser;
import co.yixiang.common.constant.CommonConstant;
import co.yixiang.common.interceptor.AuthCheck;
import co.yixiang.enums.ProductEnum;
import co.yixiang.modules.news.domain.YxNews;
import co.yixiang.modules.news.domain.YxUserNews;
import co.yixiang.modules.news.service.YxUserNewsService;
import co.yixiang.modules.news.service.mapper.YxNewsMapper;
import co.yixiang.modules.news.service.mapper.YxUserNewsMapper;
import co.yixiang.modules.news.service.vo.YxUserNewsVo;
import co.yixiang.modules.product.domain.YxStoreProduct;
import co.yixiang.modules.product.service.YxStoreProductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ycl
 * 用户消息
 * @Date2020/8/13 14:04
 * @Version V1.0
 **/
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/userNews")
@Api(value = "用户消息", tags = "用户:用户消息", description = "用户消息")
public class UserNewsController {

    private final YxUserNewsService yxUserNewsService;

    private final YxUserNewsMapper yxUserNewsMapper;

    private final YxNewsMapper yxNewsMapper;

    private final YxStoreProductService storeProductService;

    /**
     * 消息首页
     */
    @AuthCheck
    @GetMapping("/remind")
    @ApiOperation(value = "消息首页",notes = "消息首页")
    public ApiResult<Map<String, Object>> readRemind(){
        Long uid = LocalUser.getUser().getUid();
        Map<String, Object> read = new HashMap<>();

        //消息
        read.put("sysNews" , ObjectUtil.isEmpty(this.readRemind(uid , CommonConstant.SYS_TYPE))?"":this.readRemind(uid , CommonConstant.SYS_TYPE).getTitle());
        read.put("actNews", ObjectUtil.isEmpty(this.readRemind(uid , CommonConstant.ACT_TYPE))?"":this.readRemind(uid , CommonConstant.ACT_TYPE).getTitle());
        read.put("dealNews",ObjectUtil.isEmpty(this.readRemind(uid , CommonConstant.DEAL_TYPE))?"":this.readRemind(uid , CommonConstant.DEAL_TYPE).getTitle());

        //精彩推荐
//        read.put("bastList",storeProductService.getList(1,4, ProductEnum.TYPE_1.getValue()));

        return ApiResult.ok(read);
    }

    /**
     * 消息列表
     */
    @AuthCheck
    @GetMapping("/page")
    @ApiOperation(value = "消息列表",notes = "消息列表")
    public ApiResult<Page<YxUserNewsVo>> list(@RequestParam(value = "type" ,required = false) Integer type,
                                              @RequestParam(value = "page" ,required = false) Integer page,
                                              @RequestParam(value = "limit" ,required = false) Integer limit){

        if( ObjectUtil.isEmpty(page) || ObjectUtil.isEmpty(limit)){
            throw new YshopException("参数错误");
        }

        Long uid = LocalUser.getUser().getUid();

       /* List<Integer> ids = yxNewsMapper.getIdsByType(uid, type);

        //消息未读置成已读
        ids.forEach(id -> {
            YxUserNews yxUserNews = new YxUserNews();
            yxUserNews.setId(id);
            yxUserNews.setIsRead(CommonConstant.IS_READ_ONE);
            yxUserNews.setReadTime(new Date());
            yxUserNewsMapper.updateById(yxUserNews);
        });*/

        Page pageModel = new Page(page, limit);

        return ApiResult.ok(this.yxUserNewsMapper.getNewsByUserId(pageModel, uid , type));
    }

    /**
     * 删除消息
     */
    @AuthCheck
    @GetMapping("/deleteNews")
    @ApiOperation(value = "删除消息",notes = "删除消息")
    public ApiResult<String> deleteNews(@RequestParam(value = "yid" , required = false) Integer yid){
        if(ObjectUtil.isEmpty(yid)){
            return ApiResult.fail("id为空！");
        }
        yxUserNewsService.removeById(yid);
        return ApiResult.ok("删除消息成功！");
    }

    /**
     * 查看系统消息
     */
    @AuthCheck
    @GetMapping("/details")
    @ApiOperation(value = "查看系统消息",notes = "查看系统消息")
    public ApiResult<String> details(@RequestParam(value = "id" , required = false) Integer id){

        if(ObjectUtil.isEmpty(id)){
            throw new YshopException("消息id为空！");
        }
        Long uid = LocalUser.getUser().getUid();
        YxNews news = yxNewsMapper.selectById(id);
        yxUserNewsMapper.updateIsRead(id,uid);
        return ApiResult.ok(news.getContent());
    }

    /**
     * 获取每个type下的最新未读信息
     */
    public YxUserNewsVo readRemind (Long uid , Integer type){

        YxUserNewsVo yxUserNewsVo = yxUserNewsMapper.readRemind(uid, type);

        return yxUserNewsVo;
    }
}
